import 'dart:async';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mahjong/entity/mahjong/MahjongInfoEntity.dart';
import 'package:mahjong/entity/rule/MahjongRule.dart';
import 'package:mahjong/notify/CurrentUserInfo.dart';
import 'package:mahjong/page/mahjong/MahjongGameIndex.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mahjong/util/OpenOtherApp.dart';
import 'package:provider/provider.dart';

import 'dialog/PopupMenu.dart';

class MahjongDataTips extends StatefulWidget {
  MahjongDataTips({key});
  @override
  _MahjongDataTipsState createState() => _MahjongDataTipsState();
}

class _MahjongDataTipsState extends State<MahjongDataTips> {
  DateTime nowDate = new DateTime.now();
  int userCount;
  String roomCode;
  int gamesCurrentNumber;
  MahjongRule mahjongRule;
  Timer dateTimer;
  @override
  void initState() {
    super.initState();
    dateTimer =
        new Timer.periodic(new Duration(seconds: 1), (Timer timer) async {
      setState(() {
        nowDate = new DateTime.now();
      });
    });
  }

  @override
  void dispose() {
    super.dispose();
    if (dateTimer.isActive) {
      dateTimer.cancel();
    }
  }

  String dateToStr(DateTime nowDate) {
    String nowHourStr, nowMinuteStr;
    if (nowDate.hour < 10) {
      nowHourStr = "0" + nowDate.hour.toString();
    } else {
      nowHourStr = nowDate.hour.toString();
    }
    if (nowDate.minute < 10) {
      nowMinuteStr = "0" + nowDate.minute.toString();
    } else {
      nowMinuteStr = nowDate.minute.toString();
    }
    return nowHourStr + ":" + nowMinuteStr;
  }

  @override
  Widget build(BuildContext context) {
    return Consumer(builder:
        (BuildContext buildContext, MahjongInfoEntity provider, Widget child) {
      if (provider.rule == null) {
        return Container();
      }
      var userProvider = Provider.of<CurrentUserInfo>(context, listen: false);
      if (userProvider.userId == null) {
        return Container();
      }
      userCount = provider.allCardCount;
      roomCode = provider.code;
      gamesCurrentNumber = provider.gamesCurrentNumber;
      mahjongRule = provider.rule;
      return Positioned(
          top: 0,
          left: 0,
          child: Container(
              height: 30.h,
              width: ScreenUtil().screenWidth,
              decoration: BoxDecoration(color: Colors.black.withOpacity(0.2)),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Container(
                    height: 30.h,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        buildTitle(dateToStr(nowDate)),
                        Container(
                          padding: EdgeInsets.symmetric(horizontal: 15.h),
                          decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(5.h)),
                          child: Row(
                            children: [
                              Image.asset(
                                "images/mahjong/commons/bg_p1_oper_b.png",
                                fit: BoxFit.cover,
                                height: 10.h,
                              ),
                              SizedBox(width: 5.h),
                              Text(
                                userCount.toString(),
                                textAlign: TextAlign.center,
                                style: TextStyle(
                                    color: Colors.red, fontSize: 15.sp),
                              )
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                  Container(
                    height: 30.h,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        buildTitle("第" + gamesCurrentNumber.toString() + "局"),
                        PopupMenu(
                          childOnTap: () {},
                          onValueChanged: (int value) {},
                          pressType: PressType.singleClick,
                          backgroundColor: Colors.white,
                          gradient: LinearGradient(
                              begin: Alignment.topCenter,
                              end: Alignment.bottomCenter,
                              colors: [Colors.white],
                              stops: [1]),
                          direction: Axis.vertical,
                          menuHeight: 20.h,
                          menuWidth: 80.w,
                          alignment: Alignment.center,
                          actions: [
                            Text(
                              "总局数:\t" + mahjongRule.numberGame.toString(),
                              style: TextStyle(
                                  color: Colors.black,
                                  fontSize: ScreenUtil().setSp(15)),
                            ),
                            Text(
                              "总人数:\t" + mahjongRule.userCount.toString(),
                              style: TextStyle(
                                  color: Colors.black,
                                  fontSize: ScreenUtil().setSp(15)),
                            ),
                            Text(
                              "封顶:\t" + mahjongRule.turns.toString(),
                              style: TextStyle(
                                  color: Colors.black,
                                  fontSize: ScreenUtil().setSp(15)),
                            )
                          ],
                          child: Container(
                            width: 40.w,
                            child: Icon(Icons.info,
                                color: Colors.white, size: 20.h),
                          ),
                        )
                      ],
                    ),
                  ),
                  Container(
                    height: 30.h,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          roomCode,
                          style:
                              TextStyle(color: Colors.white, fontSize: 15.sp),
                        ),
                        SizedBox(width: 30.h),
                        Container(
                            width: 40.w,
                            height: 20.h,
                            alignment: Alignment.center,
                            child: TextButton(
                              onPressed: () async {
                                ClipboardData data =
                                    new ClipboardData(text: provider.code);
                                Clipboard.setData(data);
                                OpenOtherApp.openWeichat();
                                BotToast.showText(text: "复制成功");
                                /* ClipboardData clipboardData =
                                await Clipboard.getData(Clipboard.kTextPlain);
                            print('data content ${clipboardData.text}'); */
                              },
                              style: ButtonStyle(
                                  backgroundColor:
                                      MaterialStateProperty.all(Colors.orange),
                                  padding: MaterialStateProperty.all(
                                      EdgeInsetsDirectional.zero)),
                              child: Center(
                                child: Text(
                                  '复制',
                                  style: TextStyle(
                                      color: Colors.white,
                                      fontSize: 10.sp,
                                      fontFamily: "sjg"),
                                ),
                              ),
                            )),
                      ],
                    ),
                  ),
                  MahjongMenu(),
                ],
              )));
    });
  }

  Container buildTitle(String title) {
    //"第" + gamesCurrentNumber.toString() + "局"
    return Container(
        width: 50.w,
        child: Text(
          title,
          style: TextStyle(color: Colors.white, fontSize: 15.sp),
          textAlign: TextAlign.center,
        ));
  }
}
